package v1

import (
	"fmt"
	"usplatformserver/global"
	"usplatformserver/internal/model"
	"usplatformserver/internal/service"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type LicenseCodeSwagger struct {
	*model.SwaggerSuccess
	*service.LicenseCodeInfo
}

type LicenseCodeRequest struct {
	DeviceId string `form:"deviceID" binding:"required"`
}

// @Summer 获取注册码
// @Produce json
// @Param deviceID query string true "设备ID"
// @Success 200 {object} v1.LicenseCodeSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/licensecode [get]
func GetLicenseCode(c *gin.Context) {
	param := LicenseCodeRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	fmt.Println("deviceId: ", param.DeviceId)

	addr := fmt.Sprintf("tcp://%v:%d", global.ZMQSettings.Addr, global.ZMQSettings.Port)
	svc := service.New(c.Request.Context())
	md5Hex, err := svc.GetLicenseCode(addr, param.DeviceId)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetLicenseCodeFail)
		return
	}

	response.ToResponse(gin.H{
		"licenseID": md5Hex,
	})
}
